package com.woniu.controller;
import com.woniu.entity.Course;
import com.woniu.entity.User;
import com.woniu.entity.Vedio;
import com.woniu.entity.vo.Information;
import com.woniu.service.CourseInfoService;
import com.woniu.utils.ResultObj;
import com.woniu.utils.getVideoDuration;
import okhttp3.Response;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import java.net.URI;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("api/courseInfo")
@CrossOrigin(origins = "http://localhost:5173")
public class CourseInfoController {
    private  String duration;
    @Autowired
    CourseInfoService courseInfoService;
    OssController ossController = new OssController();


    /**
     * 根据id删除视频
     *
     */
    @PostMapping("DelectID")
    public ResultObj DelectID(@RequestParam("id")Integer id){
        System.out.println("删除课程id"+id);
        int a=0;
        try {
            courseInfoService.DelectID(id);
            a=1;
            System.out.println("删除课程成功");
        }catch (Exception e){
            a=2;
        }
        return ResultObj.ok(a);
    }

    /**
     * 查询课程视频信息
     * @param id
     * @return
     */
    @GetMapping("/queryCourseInfo")
    public ResultObj queryCourseInfo(@RequestParam("id")Integer id){
        List<Vedio> vedios = courseInfoService.queryCourseInfo(id);
        // 假设取列表首个元素，实际按业务逻辑调整，比如校验非空等
        System.out.println("打印结果"+vedios);
        return ResultObj.ok(vedios);
    }

    /**
     * 课程信息查询
     * @param id
     * @return
     */
    @GetMapping("/selectCourseInfo")
    public ResultObj selectCourseInfo(@RequestParam("id")Integer id){
        Course course = courseInfoService.selectCourseInfo(id);
        System.out.println("课程信息打印结果"+course);
        return ResultObj.ok(course);
    }

    @PostMapping("/amend")
    public ResultObj amend(@RequestBody Information information){
        System.out.println("-------------------------------------------------------------------------------------");
        System.out.println("修改课程信息打印结果"+information);
        System.out.println("修改课程信息打印视频信息");
        int a=0;
//        查询有没有相同的视频
        System.out.println(information.getId());
        System.out.println(information.getVideoUrl());



        //解析视频信息
        if(information.getVideoUrl()!=null){
            URI uri = URI.create(information.getVideoUrl());

//            获取视频时长
            String duration = getVideoDuration.getVideoDurationFormatted(information.getVideoUrl());
            information.setDuration(duration);
            System.out.println("视频时长"+duration);
            //获取视频大小
            long size = getVideoDuration.getVideoSize(information.getVideoUrl());
            System.out.println("视频大小字节"+ size);
            //格式化大小
            String sizeFormatted = getVideoDuration.formatFileSize(size);
            System.out.println("视频大小"+sizeFormatted);
            information.setSize(sizeFormatted);

            // 获取路径部分
            String path = uri.getPath(); // /java113-videos/redis01.mp4
            String[] pathParts = path.split("/");
            String bucketName = pathParts[1]; // java113-videos
            String objectName = pathParts[2]; // redis01.mp4
            // 获取查询参数
            Map<String, String> params = new HashMap<>();
            for (String param : uri.getQuery().split("&")) {
                String[] pair = param.split("=");
                params.put(pair[0], pair.length > 1 ? pair[1] : "");
            }
            String signature = params.get("X-Amz-Signature");
            String expires = params.get("X-Amz-Expires");
            System.out.println("Bucket Name: " + bucketName);
            System.out.println("Object Name: " + objectName);
            System.out.println("Expires: " + expires + " seconds");
            System.out.println("Signature: " + signature);
            information.setName(objectName);

        }

//更具id和名字判断视频是否存在
        Integer i = courseInfoService.selectVideoUrl(information.getId(),information.getName());

        System.out.println("视频时长"+information.getDuration());
        System.out.println("视频大小"+information.getSize());
        courseInfoService.addvideoUrl(information);
        try {
            if (i==null){
                System.out.println("添加视频信息成功");

            }else{
                System.out.println("视频存在");
                a=2;
            }
            courseInfoService.amend(information);
            System.out.println("修改课程信息成功");
        }catch (Exception e){
            a=1;
            System.out.println("修改课程信息失败");
        };
        return ResultObj.ok(a);

    }
    /**
     * 课程根据id下架
     */
    @PostMapping("/SoldOut")
    public ResultObj SoldOut(@RequestParam("id")Integer id){
        System.out.println("-------------------------------------------------------------------------------------");
        System.out.println("下架课程打印结果"+id);
        int a=0;
        courseInfoService.SoldOut(id);
        try {
        }catch (Exception e){
            a=1;
            System.out.println("下架课程失败");
        };
        return ResultObj.ok(a);

    }
    @PostMapping("/ShelvingOperation")
    public ResultObj ShelvingOperation(@RequestParam("id")Integer id){
        System.out.println("-------------------------------------------------------------------------------------");
        System.out.println("上架课程打印结果"+id);

        int a=0;
        courseInfoService.ShelvingOperation(id);
        try {
        }catch (Exception e){
            a=1;
            System.out.println("上架课程失败");
        };
        return ResultObj.ok(a);

    }
    /**
     * 课程根据id删除
     */
    @PostMapping("/Delect")
    public ResultObj Delect(@RequestParam("id")Integer id){
        System.out.println("-------------------------------------------------------------------------------------");
        System.out.println("删除课程"+id);
        int a=0;
        courseInfoService.Delect(id);
        try {
        }catch (Exception e){
            a=1;
            System.out.println("删除课程失败");
        };
        return ResultObj.ok(a);

    }
    @GetMapping("/QueryUser")
    public ResultObj QueryUser(@RequestParam("id")Integer id){
        System.out.println("-------------------------------------------------------------------------------------");
        System.out.println("查询学员信息的课程"+id);
        try {
            List<User> users = courseInfoService.QueryUser(id);
            System.out.println("查询学员信息"+users);
            return ResultObj.ok(users);
        }catch (Exception e){
            System.out.println("查询学员失败");
            return ResultObj.ok("sb");
        }
    }

}
